<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Township extends  Admin
{
	public function initialize()
    {
        $this->menu_input = [
            'all' => [
                // ['type' => 'select', 'name' => '所属', 'key' => 'parent_id','select' => [
                //         'table' => 'Township',
                //         'where'=>[
                //         	'key'=>'parent_id', 
                //         	'value'=>'0'
                //         	]
                //     ],],
                ['type' => 'text', 'name' => '镇名称', 'key' => 'name', 'verify' => 'required'],
            ],
            'add' => [
            ],
            'upload' => [
                
            ],
        ];
        if(input('p_id'))
        {
           $this->menu_input['all'][0]['name'] = '村名称';
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {
        $data = [
            ['field' => 'name', 'title' => '名称'],
            ['field' => 'type', 'title' => '类型'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        
        if(input('p_id'))
        {
           $data = [
                ['field' => 'name', 'title' => '名称'],
                ['field' => 'z_name', 'title' => '所属镇'],
                ['field' => 'type', 'title' => '类型'],
                ['title' => '操作', 'templet' => '#list_setting']
            ];
        }
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    //列表-页面
    public function index()
    {
        if(input('p_id'))
        {
            $this->assign('qt',input('p_id'));
        }
        return view();
    }
    //添加-页面
    public function add()
    {
        if(input('p_id'))
        {
            $this->assign('qt',input('p_id'));
        }
        return view();
    }
    public function index_handle(&$data)
    {
        $data['data'] = array_map(function($v){
        	if($v['parent_id'] == 0){
        		$v['type'] = '镇';
        	}else{
        		$v['type'] = '村';
        	}
        	if(input('p_id'))
            {
            	$v['z_name'] = $this->model->where('id',$v['parent_id'])->find()->toArray()['name'];
            }
            return $v;
        },$data['data']);
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
    //列表-数据接口
    public function datalist()
    {
        if (is_array($this->with)) {
            $with = $this->with;
        } else {
            $with = [];
        }
        $data = param();
        unset($data['page']);
        unset($data['limit']);
        unset($data['parent']);
        foreach ($data as $k => $v) {
            if (in_array($k, ['name', 'title'])) {
                $this->where[] = [$k, 'like', '%' . $v . '%'];
            } elseif ($k == 'create_time') {
                $arr = b_arr($v, '-');
                $first = b_str([$arr[0], $arr[1], $arr[2]], '-');
                $last = b_str([$arr[3], $arr[4], $arr[5]], '-');
                $this->where[] = ['create_time', '>=', trim($first)];
                $this->where[] = ['create_time', '<=', trim($last)];
            } elseif($k == 'p_id'){
                
            }else {
                $this->where[] = [$k, '=', $v];
            }
        }
        if ($this->parent_model && input('parent')) {
            if ($this->request->controller() == 'Menu') {
                $this->where[] = [lcfirst($this->parent_model) . 'id', '=', input('parent')];
            } else {
                $this->where[] = [lcfirst($this->parent_model) . '_id', '=', input('parent')];
            }
        }
        $order = $this->order ?: 'id desc';
        $this->index_where($this->where);
        if(!empty($this->where)){
            $this->where = array_values($this->where);
        }
        if(!input('p_id')){
            $this->where[] = ['parent_id','=','0'];
        }else{
            $this->where[] = ['parent_id','=',input('p_id')];
        }
        $list = $this->model->where($this->where)->order($order)->with($with)->paginate($this->limit)->toArray();
        $this->index_handle($list);
        return layui($list['data'], $list['total']);
    }
}
